/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制构建AB包的窗口
* CreateTime：2020-07-10 13:03:57
* Version：1.0
* Modify Recorder：
*************************************************/

using System.IO;
using UnityEditor;
using UnityEngine;
using TinyToolKit;
using TinyToolKit.AssetLoad;
using UnityEditorInternal;

namespace TinyToolKitEditor
{
    public class DrawWindowABBuild : DrawWindowBase 
    {
        private SerializedObject _serializedObject;
        private ReorderableList _allPrefabDirPath;
        private ReorderableList _allAssetFileDirPath;
        private bool _isFoldoutAdvancedSettings = true;
        private TinyToolEditorSettingAsset _toolSetting; 
        private TAssetBundleManager _assetBundleManager;

        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolEditorSettingAsset) setting;
            _serializedObject = new SerializedObject(setting);

            _allPrefabDirPath = TinyToolWindowUtil.GetReorderableList(_serializedObject, "allPrefabDirPath", "预制体文件夹路径","路径");
            _allAssetFileDirPath = TinyToolWindowUtil.GetReorderableList(_serializedObject, "allAssetFileDirPath", "资源文件夹路径","路径");
        }

        protected override void DrawInternalMainGUI()
        {
            _serializedObject.Update();
            
            _allPrefabDirPath.DoLayoutList();
            EditorGUILayout.Space();
            _allAssetFileDirPath.DoLayoutList();
            
            EditorGUILayout.Space();
            _toolSetting.assetBundleVariant = TinyToolWindowUtil.DrawTextField("AB包后缀名：", _toolSetting.assetBundleVariant);
            _toolSetting.buildTarget = (BuildPlatform)TinyToolWindowUtil.DrawEnumPopup("构建平台：", _toolSetting.buildTarget);
            _toolSetting.compressType = (ABCompressType)TinyToolWindowUtil.DrawEnumPopup("压缩模式：", _toolSetting.compressType);
            _toolSetting.dataFormat = (DataFormat)TinyToolWindowUtil.DrawEnumPopup("配置表类型：", _toolSetting.dataFormat);

            EditorGUILayout.BeginHorizontal();
            _toolSetting.buildSavePath = EditorGUILayout.TextField("输出路径：", _toolSetting.buildSavePath);
            if (GUILayout.Button(EditorGUIUtility.FindTexture("Folder Icon"), GUILayout.MaxWidth(40), GUILayout.MaxHeight(20)))
            {
                string savePath = EditorUtility.OpenFolderPanel("AB包存储路径", "Assets", "StreamingAssets");
                if (savePath.IsNullOrEmpty() == false)
                {
                    _toolSetting.buildSavePath = savePath;
                } 
            }
            EditorGUILayout.EndHorizontal();
            
            EditorGUILayout.Space();
            _isFoldoutAdvancedSettings = EditorGUILayout.Foldout(_isFoldoutAdvancedSettings,"高级设置");
            if (_isFoldoutAdvancedSettings)
            {
                _toolSetting.deleteUnusedAB = EditorGUILayout.Toggle("  删除未使用的AB包：", _toolSetting.deleteUnusedAB);
                _toolSetting.isClearAllABName = EditorGUILayout.Toggle("  结束后清除AB包名：", _toolSetting.isClearAllABName);
                _toolSetting.deleteABManifest = EditorGUILayout.Toggle("  删除Manifest文件：", _toolSetting.deleteABManifest); 
                _toolSetting.spawnVersion = EditorGUILayout.Toggle("  输出AB版本文件：", _toolSetting.spawnVersion); 
                _toolSetting.encrypt = EditorGUILayout.Toggle("  AB包资源加密：", _toolSetting.encrypt);
            }
            EditorGUILayout.Space();

            if (TinyToolWindowUtil.DrawButton("构建"))
            {
                if (_toolSetting.buildSavePath.IsNullOrEmpty() == false)
                {
                    if (Directory.Exists(_toolSetting.buildSavePath) == false)
                        TinyToolWindowUtil.DrawWarnningDialog("存储路径不是有效路径！");
                    else
                    {
                        BuildAssetBundle.Build(_toolSetting,GetBuildTarget(_toolSetting.buildTarget),GetCompressType(_toolSetting.compressType));
                        GUIUtility.ExitGUI();
                    }
                }
                else
                {
                    TinyToolWindowUtil.DrawWarnningDialog("存储路径不能为空！");
                }
            }
            
            _serializedObject.ApplyModifiedProperties();
        }

        private static BuildTarget GetBuildTarget(BuildPlatform platform)
        {
            switch (platform)
            {
                case BuildPlatform.ActiveBuildTarget:
                    return EditorUserBuildSettings.activeBuildTarget;
                case BuildPlatform.Android:
                    return BuildTarget.Android;
                case BuildPlatform.Switch:
                    return BuildTarget.Switch;
                case BuildPlatform.iOS :
                    return BuildTarget.iOS;
                case BuildPlatform.StandaloneLinux64:
                    return BuildTarget.StandaloneLinux64;
                case BuildPlatform.StandaloneWindows64:
                    return BuildTarget.StandaloneWindows64;
                case BuildPlatform.XboxOne:
                    return BuildTarget.XboxOne;
                case BuildPlatform.WebGL:
                    return BuildTarget.WebGL;
                case BuildPlatform.StandaloneOSX:
                    return BuildTarget.StandaloneOSX;
                case BuildPlatform.WSAPlayer:
                    return BuildTarget.WSAPlayer;
                default:
                    return BuildTarget.StandaloneWindows64;
            }
        }

        private BuildAssetBundleOptions GetCompressType(ABCompressType compressType)
        {
            switch (compressType)
            {
                case ABCompressType.None:
                    return BuildAssetBundleOptions.UncompressedAssetBundle;
                case ABCompressType.LZ4:
                    return BuildAssetBundleOptions.ChunkBasedCompression;
                case ABCompressType.LZMA:
                    return BuildAssetBundleOptions.None;
                default:
                    return BuildAssetBundleOptions.ChunkBasedCompression;
            }
        }
    } 
}

